/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package uimigration.WidgetsModel.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;
import org.eclipse.emf.ecore.util.EDataTypeUniqueEList;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;

import uimigration.Interactions.Behaviour;
import uimigration.WidgetsModel.Event;
import uimigration.WidgetsModel.Property;
import uimigration.WidgetsModel.Widget;
import uimigration.WidgetsModel.WidgetsModelPackage;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Widget</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link uimigration.WidgetsModel.impl.WidgetImpl#getName <em>Name</em>}</li>
 *   <li>{@link uimigration.WidgetsModel.impl.WidgetImpl#getId <em>Id</em>}</li>
 *   <li>{@link uimigration.WidgetsModel.impl.WidgetImpl#getProperties <em>Properties</em>}</li>
 *   <li>{@link uimigration.WidgetsModel.impl.WidgetImpl#getEvents <em>Events</em>}</li>
 *   <li>{@link uimigration.WidgetsModel.impl.WidgetImpl#getContainsWidget <em>Contains Widget</em>}</li>
 *   <li>{@link uimigration.WidgetsModel.impl.WidgetImpl#isIsContainer <em>Is Container</em>}</li>
 *   <li>{@link uimigration.WidgetsModel.impl.WidgetImpl#getCardinality <em>Cardinality</em>}</li>
 *   <li>{@link uimigration.WidgetsModel.impl.WidgetImpl#isIsRoot <em>Is Root</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class WidgetImpl extends EObjectImpl implements Widget {
	/**
	 * The default value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected static final String NAME_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected String name = NAME_EDEFAULT;

	/**
	 * The default value of the '{@link #getId() <em>Id</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getId()
	 * @generated
	 * @ordered
	 */
	protected static final int ID_EDEFAULT = 0;

	/**
	 * The cached value of the '{@link #getId() <em>Id</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getId()
	 * @generated
	 * @ordered
	 */
	protected int id = ID_EDEFAULT;

	/**
	 * The cached value of the '{@link #getProperties() <em>Properties</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getProperties()
	 * @generated
	 * @ordered
	 */
	protected EList<Property> properties;

	/**
	 * The cached value of the '{@link #getEvents() <em>Events</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getEvents()
	 * @generated
	 * @ordered
	 */
	protected EList<Event> events;
	
	/**
	 * The cached value of the '{@link #getContainsWidget() <em>Contains Widget</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getContainsWidget()
	 * @generated
	 * @ordered
	 */
	protected EList<Widget> containsWidget;

	/**
	 * The default value of the '{@link #isIsContainer() <em>Is Container</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isIsContainer()
	 * @generated
	 * @ordered
	 */
	protected static final boolean IS_CONTAINER_EDEFAULT = false;

	/**
	 * The cached value of the '{@link #isIsContainer() <em>Is Container</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isIsContainer()
	 * @generated
	 * @ordered
	 */
	protected boolean isContainer = IS_CONTAINER_EDEFAULT;

	/**
	 * The cached value of the '{@link #getCardinality() <em>Cardinality</em>}' attribute list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getCardinality()
	 * @generated
	 * @ordered
	 */
	protected EList<Integer> cardinality;

	/**
	 * The default value of the '{@link #isIsRoot() <em>Is Root</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isIsRoot()
	 * @generated
	 * @ordered
	 */
	protected static final boolean IS_ROOT_EDEFAULT = false;

	/**
	 * The cached value of the '{@link #isIsRoot() <em>Is Root</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isIsRoot()
	 * @generated
	 * @ordered
	 */
	protected boolean isRoot = IS_ROOT_EDEFAULT;
	
    /**
     * The cached value of the '{@link #getBehaviour() <em>Behavior</em>}' containment reference list.
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @see #getBehavior()
     * @generated NOT 
     * @ordered 
     */
    protected List<Behaviour> behaviours;


	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected WidgetImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return WidgetsModelPackage.Literals.WIDGET;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getName() {
		return name;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setName(String newName) {
		String oldName = name;
		name = newName;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, WidgetsModelPackage.WIDGET__NAME, oldName, name));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public int getId() {
		return id;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setId(int newId) {
		int oldId = id;
		id = newId;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, WidgetsModelPackage.WIDGET__ID, oldId, id));
	}



	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Property> getProperties() {
		if (properties == null) {
			properties = new EObjectContainmentEList<Property>(Property.class, this, WidgetsModelPackage.WIDGET__PROPERTIES);
		}
		return properties;
	}
	
    /**
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated NOT
     */
    public int getContainsWidgetLength() {
            return containsWidget == null ? 0 : containsWidget.size();
    }

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Event> getEvents() {
		if (events == null) {
			events = new EObjectContainmentEList<Event>(Event.class, this, WidgetsModelPackage.WIDGET__EVENTS);
		}
		return events;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Widget> getContainsWidget() {
		if (containsWidget == null) {
			containsWidget = new EObjectContainmentEList<Widget>(Widget.class, this, WidgetsModelPackage.WIDGET__CONTAINS_WIDGET);
		}
		return containsWidget;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean isIsContainer() {
		return isContainer;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setIsContainer(boolean newIsContainer) {
		boolean oldIsContainer = isContainer;
		isContainer = newIsContainer;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, WidgetsModelPackage.WIDGET__IS_CONTAINER, oldIsContainer, isContainer));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Integer> getCardinality() {
		if (cardinality == null) {
			cardinality = new EDataTypeUniqueEList<Integer>(Integer.class, this, WidgetsModelPackage.WIDGET__CARDINALITY);
		}
		return cardinality;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean isIsRoot() {
		return isRoot;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setIsRoot(boolean newIsRoot) {
		boolean oldIsRoot = isRoot;
		isRoot = newIsRoot;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, WidgetsModelPackage.WIDGET__IS_ROOT, oldIsRoot, isRoot));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Behaviour getBehaviors() {
		// TODO: implement this method
		// Ensure that you remove @generated or mark it @generated NOT
		throw new UnsupportedOperationException();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd,
			int featureID, NotificationChain msgs) {
		switch (featureID) {
			case WidgetsModelPackage.WIDGET__PROPERTIES:
				return ((InternalEList<?>)getProperties()).basicRemove(otherEnd, msgs);
			case WidgetsModelPackage.WIDGET__EVENTS:
				return ((InternalEList<?>)getEvents()).basicRemove(otherEnd, msgs);
			case WidgetsModelPackage.WIDGET__CONTAINS_WIDGET:
				return ((InternalEList<?>)getContainsWidget()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case WidgetsModelPackage.WIDGET__NAME:
				return getName();
			case WidgetsModelPackage.WIDGET__ID:
				return getId();
			case WidgetsModelPackage.WIDGET__PROPERTIES:
				return getProperties();
			case WidgetsModelPackage.WIDGET__EVENTS:
				return getEvents();
			case WidgetsModelPackage.WIDGET__CONTAINS_WIDGET:
				return getContainsWidget();
			case WidgetsModelPackage.WIDGET__IS_CONTAINER:
				return isIsContainer();
			case WidgetsModelPackage.WIDGET__CARDINALITY:
				return getCardinality();
			case WidgetsModelPackage.WIDGET__IS_ROOT:
				return isIsRoot();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case WidgetsModelPackage.WIDGET__NAME:
				setName((String)newValue);
				return;
			case WidgetsModelPackage.WIDGET__ID:
				setId((Integer)newValue);
				return;
			case WidgetsModelPackage.WIDGET__PROPERTIES:
				getProperties().clear();
				getProperties().addAll((Collection<? extends Property>)newValue);
				return;
			case WidgetsModelPackage.WIDGET__EVENTS:
				getEvents().clear();
				getEvents().addAll((Collection<? extends Event>)newValue);
				return;
			case WidgetsModelPackage.WIDGET__CONTAINS_WIDGET:
				getContainsWidget().clear();
				getContainsWidget().addAll((Collection<? extends Widget>)newValue);
				return;
			case WidgetsModelPackage.WIDGET__IS_CONTAINER:
				setIsContainer((Boolean)newValue);
				return;
			case WidgetsModelPackage.WIDGET__CARDINALITY:
				getCardinality().clear();
				getCardinality().addAll((Collection<? extends Integer>)newValue);
				return;
			case WidgetsModelPackage.WIDGET__IS_ROOT:
				setIsRoot((Boolean)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case WidgetsModelPackage.WIDGET__NAME:
				setName(NAME_EDEFAULT);
				return;
			case WidgetsModelPackage.WIDGET__ID:
				setId(ID_EDEFAULT);
				return;
			case WidgetsModelPackage.WIDGET__PROPERTIES:
				getProperties().clear();
				return;
			case WidgetsModelPackage.WIDGET__EVENTS:
				getEvents().clear();
				return;
			case WidgetsModelPackage.WIDGET__CONTAINS_WIDGET:
				getContainsWidget().clear();
				return;
			case WidgetsModelPackage.WIDGET__IS_CONTAINER:
				setIsContainer(IS_CONTAINER_EDEFAULT);
				return;
			case WidgetsModelPackage.WIDGET__CARDINALITY:
				getCardinality().clear();
				return;
			case WidgetsModelPackage.WIDGET__IS_ROOT:
				setIsRoot(IS_ROOT_EDEFAULT);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case WidgetsModelPackage.WIDGET__NAME:
				return NAME_EDEFAULT == null ? name != null : !NAME_EDEFAULT.equals(name);
			case WidgetsModelPackage.WIDGET__ID:
				return id != ID_EDEFAULT;
			case WidgetsModelPackage.WIDGET__PROPERTIES:
				return properties != null && !properties.isEmpty();
			case WidgetsModelPackage.WIDGET__EVENTS:
				return events != null && !events.isEmpty();
			case WidgetsModelPackage.WIDGET__CONTAINS_WIDGET:
				return containsWidget != null && !containsWidget.isEmpty();
			case WidgetsModelPackage.WIDGET__IS_CONTAINER:
				return isContainer != IS_CONTAINER_EDEFAULT;
			case WidgetsModelPackage.WIDGET__CARDINALITY:
				return cardinality != null && !cardinality.isEmpty();
			case WidgetsModelPackage.WIDGET__IS_ROOT:
				return isRoot != IS_ROOT_EDEFAULT;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (name: ");
		result.append(name);
		result.append(", id: ");
		result.append(id);
		result.append(", isContainer: ");
		result.append(isContainer);
		result.append(", cardinality: ");
		result.append(cardinality);
		result.append(", isRoot: ");
		result.append(isRoot);
		result.append(')');
		return result.toString();
	}

	/**
	 * Sets the value of the '{@link uimigration.WidgetsModel.Widget#isIsContainer <em>Is Container</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @param  
	 * @see #isIsContainer()
	 * @generated NOT
	 */
	public List<Behaviour> getBehavior() {
		if(behaviours==null){
				behaviours = new ArrayList<Behaviour>();
			List<Behaviour> behlist = new ArrayList<Behaviour>();
			if (events != null)
				for (Event e :events)
				{
					if(e.getEventBehaviourLength()>0)
						for (int i=0;i<e.getEventBehaviourLength();i++)
							behlist.add(e.getEventBehaviour(i));
				}
			Set<String> doubleElt = new HashSet<String>();
			for (Behaviour b : behlist) {
				String name = "";
				if (b.getComposedBehaviors() == null  )
					name = b.getClass().getCanonicalName()
							+ b.getIdBehavior();
				else {
					Behaviour bi = b.getComposedBehaviors();
					while (bi != null) {
						name += b.getClass().getCanonicalName()
								+ bi.getIdBehavior();
						bi = bi.getComposedBehaviors();
					}
				}
				if (!doubleElt.contains(name)) {
					behaviours.add(b);
					doubleElt.add(name);
				}
			}
		}
		//System.out.println(this.name +" behaviors "+ behaviours.toString() +"\n" );
		return behaviours;
	}

} //WidgetImpl
